<?php
/**
 * 海外仓-移库库存调整
 * (model)
 */
namespace App\Models\OverSeasBa;

use App\Models\Basics\SysPlatAccount;
use App\Models\Product\ProductSku;
use App\Util\User;
use Illuminate\Database\Eloquent\Model;
class RemovaleAdjustFNSku extends Model
{
    protected $table = 'os_b_removale_adjust_fnsku';

    protected $guarded = [];

    public static $_type = [
        1 => '调整',
        2 => '新增'
    ];

    public function warehouse(){
        return $this->belongsTo(Warehouse::class,'warehouse_id');
    }

    public function sku(){
        return $this->belongsTo(ProductSku::class,'psku_id');
    }

    public static function getDownloadData($re){
        $account = [];
        $warehouse = [];
        $user = [];
        foreach ($re as $row){
            $temp = [];
            $accountId = $row->account_id ?? -1;
            if(!array_key_exists($accountId,$account)){
                $account[$accountId] = SysPlatAccount::getAccountById($accountId);
            }
            $temp['account_id'] = $account[$accountId];
            if(!array_key_exists($row->warehouse_id,$warehouse)){
                $warehouse[$row->warehouse_id] = Warehouse::getCodeById($row->warehouse_id);
            }
            $temp['warehouse_id'] = $warehouse[$row->warehouse_id];
            $temp['sku'] = $row->sku->psku_code ?? '';
            $temp['fnsku'] = $row->fnsku;
            $temp['b_num'] = $row->b_num;
            $temp['a_num'] = $row->a_num;
            $temp['psku_cn'] = $row->sku->psku_cn ?? '';
            $temp['type'] = self::$_type[$row->type];
            if(!array_key_exists($row->auser_id,$user)){
                $user[$row->auser_id] = User::getUsernameById($row->auser_id);
            }
            $temp['auser_id'] = $user[$row->auser_id];
            $temp['created_at'] = $row->created_at;
            yield $temp;
        }
    }
}
